package com.ww.springboot.boot.utils;

import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 基于 itext7
 *
 * @author wanwei
 * @since 2021-01-09 09:08
 */
@Slf4j
public class PdfUtils2 {

	public static byte[] zoom(byte[] bytes) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		PdfWriter writer = null;
		PdfReader reader = null;
		try {
			writer = new PdfWriter(outputStream);
			reader = new PdfReader(new ByteArrayInputStream(bytes));
			// Creating a PdfDocument objects
			PdfDocument destpdf = new PdfDocument(writer);
			PdfDocument srcPdf = new PdfDocument(reader);
			for(int i = 1;i<=srcPdf.getNumberOfPages();i++){
				// Opening a page from the existing PDF
				PdfPage origPage = srcPdf.getPage(i);
				float height = origPage.getCropBox().getHeight();

				// Adding a page to destination Pdf
				PdfPage page = destpdf.addNewPage(new PageSize(origPage.getPageSize()));
				page.setCropBox(origPage.getCropBox());
				// Scaling the image in a Pdf page
				AffineTransform transformationMatrix = AffineTransform.getScaleInstance(
					1,
					0.96);
				// Shrink original page content using transformation matrix
				PdfCanvas canvas = new PdfCanvas(page);
				canvas.concatMatrix(transformationMatrix);
				// Add the object to the canvas
				PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
				canvas.addXObject(pageCopy, 0, (float) (height * (1 - 0.96)));
			}
			// Creating a Document object
			Document doc = new Document(destpdf);
			// Closing the document
			doc.close();
			return outputStream.toByteArray();
		} catch (Exception e) {
			log.info("pdf生成失败", e);
			throw new RuntimeException(e);
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
				if (writer != null) {
					writer.close();
				}
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static byte[] zoom(byte[] bytes, Rectangle rectangle) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		PdfWriter writer = null;
		PdfReader reader = null;
		try {
			writer = new PdfWriter(outputStream);
			reader = new PdfReader(new ByteArrayInputStream(bytes));
			// Creating a PdfDocument objects
			PdfDocument destpdf = new PdfDocument(writer);
			PdfDocument srcPdf = new PdfDocument(reader);
			for (int i = 1; i <= srcPdf.getNumberOfPages(); i++) {
				float zoomY = 1f;
				float zoomX = 1f;
				// Opening a page from the existing PDF
				PdfPage origPage = srcPdf.getPage(i);
				Rectangle rectangleOld = origPage.getCropBox();
				AffineTransform transformationMatrix;

				//缩放面单
				//判断是10*10 还是10*15 还是 15*10
				zoomY = 1 / (rectangleOld.getHeight() / rectangle.getHeight());
				zoomX = 1 / (rectangleOld.getWidth() / rectangle.getWidth());
				if (zoomY > 1) {
					zoomY = 1f;
				}
				if (zoomX > 1) {
					zoomX = 1f;
				}
				transformationMatrix = AffineTransform.getScaleInstance(
						zoomX,
						zoomY * 0.96);

				float height = rectangle.getHeight();
				//将新建PDF覆盖到原PDF上
				PdfPage page = destpdf.addNewPage(new PageSize(rectangle));
				page.setCropBox(origPage.getCropBox());
				PdfCanvas canvas = new PdfCanvas(page);
				canvas.concatMatrix(transformationMatrix);
				// Add the object to the canvas
				PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
				//覆盖起点坐标 将纵坐标往上移动 留出出库编号位置
				canvas.addXObject(pageCopy, 0, (float) (height * (0.04 / zoomY)));
			}
			// Creating a Document object
			Document doc = new Document(destpdf);
			// Closing the document
			doc.close();
			return outputStream.toByteArray();
		} catch (Exception e) {
			log.info("pdf生成失败", e);
			throw new RuntimeException(e);
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
				if (writer != null) {
					writer.close();
				}
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
